//
//  DetailsWebViewController.m
//  aMoment
//
//  Created by dllo on 15/10/18.
//  Copyright © 2015年 LuckyHan. All rights reserved.
//

#import "DetailsWebViewController.h"
#import "AllHeaderFiles.h"

@interface DetailsWebViewController ()<UIWebViewDelegate>
@property(nonatomic, retain)UIWebView *webView;
@property(nonatomic, retain)NSString *detailURLString;
@property(nonatomic, retain)NSString *detailBodyString;
@property(nonatomic, assign)NSInteger type;
@property(nonatomic, retain)TimelineDetailsTopView *topViewOfType1;
@property(nonatomic, retain)StoreDetailsTopView *topViewOfType2;
@end

@implementation DetailsWebViewController

- (void)dealloc
{
    [_webView release];
    [_topViewOfType1 release];
    [_topViewOfType2 release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self getData];
}

- (void)loadView
{
    [super loadView];
    [self judgeRequirement];
    
    //创建topView
    if (_type == 0) {
        
    }
    else if (_type == 1) {
        self.topViewOfType1 = [[TimelineDetailsTopView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 90)];
        _topViewOfType1.backgroundColor = [UIColor whiteColor];
    }
    else {
        self.topViewOfType2 = [[StoreDetailsTopView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 162)];
        _topViewOfType1.backgroundColor = [UIColor whiteColor];
    }
    
    //创建webView
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    _webView.delegate = self;
    if (_type) {
        [_webView.scrollView.subviews objectAtIndex:0].frame = CGRectMake(0, _topViewOfType1.Height, SCREEN_WIDTH, _webView.Height - _topViewOfType1.Height);
        [_webView.scrollView addSubview:_topViewOfType1];
        [_topViewOfType1 release];
    }
    [self.view addSubview:_webView];
    [_webView release];
}

#pragma mark 判断样式及urlString
- (void)judgeRequirement
{
    if ([_urlString isEqualToString:URLSTRING_HOME]) {
        _detailURLString = @"http://api2.pianke.me/article/info";
        if ([_enName isEqualToString:@"Timeline"]) {
            _type = 1;
        } else {
            _type = 2;
        }
    }
    else if ([_urlString isEqualToString:URLSTRING_READ]) {
        _detailURLString = @"http://api2.pianke.me/article/info";
    }
    else if ([_urlString isEqualToString:URLSTRING_COMMUNITY_TOPIC]) {
        _detailURLString = @"http://api2.pianke.me/group/posts_info";
    }
    else if ([_urlString isEqualToString:URLSTRING_TIMELINE]) {
        _detailURLString = @"http://api2.pianke.me/timeline/info";
    }
    else if ([_urlString isEqualToString:URLSTRING_STORE]) {
        _detailURLString = @"http://api2.pianke.me/group/posts_info";
    }
    _detailBodyString = [NSString stringWithFormat:@"contentid=%@&client=2", _contentId];
}

#pragma mark 获取数据
- (void)getData
{
    [NetworkRequest getDataWithURLString:_detailURLString BodyString:_detailBodyString DataBlock:^(id data) {
        NSDictionary *dataDict = [(NSDictionary *)data objectForKey:@"data"];
        NSString *htmlString = [NSString string];
        if (!_type) {
            htmlString = [self deleteHyperlinkStringWithHtmlString:[dataDict objectForKey:@"html"]];
        }
        else {
            if (_type == 1) {
                htmlString = [self deleteHyperlinkStringWithHtmlString:[dataDict objectForKey:@"html"]];
//                [_webView loadHTMLString:htmlString baseURL:nil];
                [_topViewOfType1.userImageView setImageWithURL:[NSURL URLWithString:[[dataDict objectForKey:@"userinfo"] objectForKey:@"icon"]]];
                _topViewOfType1.usernameLabel.text = [[dataDict objectForKey:@"userinfo"] objectForKey:@"uname"];
                _topViewOfType1.addTimeLabel.text = [dataDict objectForKey:@"addtime_f"];
            }
            if (_type == 2) {
                NSDictionary *postsInfoDict = [dataDict objectForKey:@"postsinfo"];
                htmlString = [self deleteHyperlinkStringWithHtmlString:[postsInfoDict objectForKey:@"html"]];
                _topViewOfType2.postsInfoDict = postsInfoDict;
            }
        }
        [_webView loadHTMLString:htmlString baseURL:nil];
        NSLog(@"%@", htmlString);
    }];
}

#pragma mark 删除html里的超链接
- (NSString *)deleteHyperlinkStringWithHtmlString:(NSString *)htmlString
{
    NSArray *htmlStringArray = [htmlString componentsSeparatedByString:@"<a "];
    htmlString = [NSString string];
    for (NSString *str in htmlStringArray) {
        htmlString = [htmlString stringByAppendingFormat:@"%@", [str componentsSeparatedByString:@"</a>"].lastObject];
    }
    return htmlString;
}

#pragma mark 协议方法html加载完毕
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//------------------------------------------------------------
    //修改JS语言
    //定义JS字符串(JavaScript)
    NSString *script = [NSString stringWithFormat:@"var script = document.createElement('script');"
                        "script.type = 'text/javascript';"
                        "script.text = \"function ResizeImages() { "
                        "var myimg;"
                        "var maxwidth = %f;"//屏幕宽度
                        "for (i = 0; i < document.images.length; i++) {"
                        "myimg = document.images[i];"
                        "myimg.height = maxwidth / (myimg.width / myimg.height);"
                        "myimg.width = maxwidth;"
                        "}"
                        "}\";"
                        "document.getElementsByTagName('p')[0].appendChild(script);", SCREEN_WIDTH - 15];
    //添加JS
    [webView stringByEvaluatingJavaScriptFromString:script];
    //添加调用JS执行的语句
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
//------------------------------------------------------------
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
